package com.lavida.project.system.goods.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.page.TableDataInfo;
import com.lavida.project.system.goods.domain.Goods;
import com.lavida.project.system.goods.service.GoodsService;
import com.lavida.project.system.goods.service.SubGoodsService;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;

@Controller
@RequestMapping("system/goods")
public class GoodsController extends BaseController{
	private String prefix = "system/goods";
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private SubGoodsService subGoodsService;
	@Autowired
	private IUserService userService;
	@RequiresPermissions("data:goods:view")
	@RequestMapping()
	public String goods(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return prefix + "/goods";
	}
	@RequiresPermissions("data:goods:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Goods goods) {
		startPage();		
		List<Goods> selectGoods = goodsService.selectGoods(goods);
		return getDataTable(selectGoods);
	}
	//插入用品的信息
	@RequiresPermissions("data:goods:add")
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@PostMapping("/showAddGoods")
	@ResponseBody
	public Map<String,Object> showAddGoods(Goods goods) {
		Map<String, Object> result = new HashMap<>();
		try {
			goodsService.insertGoods(goods);
        	result.put("status","0");
        }catch (Exception e) {
			// TODO: handle exception
        	result.put("status", "20");
        	result.put("msg", e);
		}	
		return result;
	}
	@RequiresPermissions("data:goods:delete")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
	@ResponseBody
	public Map<String, String> delete(@RequestParam("ids[]")int[] ids) {
		Map<String, String> result = new HashMap<String, String>();
		subGoodsService.deleteSubgoodsById(ids);
		goodsService.deleteGoods(ids);
		result.put("msg", "删除成功");
		return result;
	}
	@PostMapping("/getGoods")
	@ResponseBody
	public Map<String, Object> getGoods(int id){
		Map<String, Object> result = new HashMap<>();
		Goods good = goodsService.getGoods(id);
		result.put("good", good);
		return result;
	}
	@RequiresPermissions("data:goods:update")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/showUpdateGoods")
	@ResponseBody
	public Map<String, Object> updateGoods(Goods goods) {
		Map<String, Object> result = new HashMap<>();
		try {
			goodsService.updateGoods(goods);
        	result.put("status","0");
        }catch (Exception e) {
			// TODO: handle exception
        	result.put("status", "20");
        	result.put("msg", e);
		}	
		return result;
	}
}
